package org.csu.onlineedubackend.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.csu.onlineedubackend.entity.Lesson;

import java.util.List;

@Mapper
public interface LessonMapper extends BaseMapper<Lesson> {

//    @Select("SELECT * FROM lesson WHERE chapter_id = #{chapterId} ORDER BY sort_order")
//    List<Lesson> getLessonsByCourseId(Integer chapterId);

    //教师端使用
    @Select("SELECT COUNT(*) FROM lesson l " +
            "JOIN chapter c ON l.chapter_id = c.id " +
            "WHERE c.course_id = #{courseId}")
    int countLessonsByCourseId(@Param("courseId") Integer courseId);

    @Select("""
        SELECT l.*
        FROM lesson l
        JOIN chapter c ON l.chapter_id = c.id
        WHERE c.course_id = #{courseId}
        ORDER BY c.sort_order, l.sort_order
    """)
    List<Lesson> getLessonsByCourseId(Integer courseId);
    
    /**
     * 根据章节ID查询课时列表
     * @param chapterId 章节ID
     * @return 课时列表
     */
    @Select("SELECT * FROM lesson WHERE chapter_id = #{chapterId} ORDER BY sort_order ASC")
    List<Lesson> selectByChapterId(Integer chapterId);


}
